package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.QueryOrderStateEntity;
import com.zw.yzk.kabu.data.network.task.QueryOrderStateTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.view.view.WXPayView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/16.
 */

public class WXPayPresenter implements Presenter {

    private UseCase querOrderState;
    private AppCompatActivity appCompatActivity;
    private WXPayView wxPayView;

    @Inject
    public WXPayPresenter(@Named("queryOrderUseCase") UseCase querOrderState, AppCompatActivity appCompatActivity) {
        this.querOrderState = querOrderState;
        this.appCompatActivity = appCompatActivity;
    }

    public void setView(WXPayView view) {
        wxPayView = view;
    }

    public void checkOrderState(String tradeId) {
        querOrderState.execute(new QueryOrderStateSubscriber(appCompatActivity), new QueryOrderStateTask(tradeId));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        querOrderState.unSubscribe();
    }

    private class QueryOrderStateSubscriber extends BaseSubscriber<QueryOrderStateEntity> {

        public QueryOrderStateSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public void onNext(QueryOrderStateEntity entity) {
            super.onNext(entity);
            if (entity.result.result == 1) {
                wxPayView.queryOrderStateSuccess();
            } else {
                wxPayView.queryOrderStateFailed();
            }
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            wxPayView.queryOrderStateFailed();
        }

        @Override
        public String addErrorMessage() {
            return appCompatActivity.getResources().getString(R.string.check_order_state_by_yourself);
        }
    }
}
